
class Solution {
public:
    int maximalRectangle(vector<vector<char>>& matrix) {
        int m = matrix.size();
        int n = matrix[0].size();
        vector<int> heights(n + 1, 0); 
        int maxArea = 0;

        for (int row = 0; row < m; row++) {
            stack<int> stk;       
            for (int col = 0; col <= n; col++) {
                if (col < n && matrix[row][col] == '1') { 
                    heights[col] += 1;
                }
                else {
                    heights[col] = 0;               
                }
               
                
                while (!stk.empty() && heights[col] < heights[stk.top()]) {
                    int height = heights[stk.top()]; 
                    stk.pop();
                    int leftLessMin = stk.empty() ? -1 : stk.top(); 
                    int RightLessMin = col; 
                    int area = (RightLessMin - leftLessMin - 1) * height;
                    maxArea = max(area, maxArea);
                }
                stk.push(col);                
            }
        }

        return maxArea;
    }
};
